<?php
    function connect()
    {
        try {
                    $conn = new PDO ("sqlsrv:server = tcp:djnrgfij6m.database.windows.net,1433; Database = Grocery-Express", "gexpress", "Groceryexpress2014"); 
                    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                    echo "Connection Established!".'<br/>';
        }
        catch ( PDOException $e ) 
        {  
                    print( "Error connecting to SQL Server.");
                    die(print_r($e));
        }

      if(!empty($_POST)) {
try {
     $username = $_POST['username'];
     $password = $_POST['password'];
     $emailadd = $_POST['emailadd'];
     $mobile = $_POST['mobileno'];
;
    // Insert data
     $sql_insert = "INSERT INTO registeredusers (username,password,emailadd,mobileno) VALUES (?,?,?)";

    $stmt = $conn->prepare($sql_insert);
    $stmt->bindValue(1, $username);
    $stmt->bindValue(2, $password);
    $stmt->bindValue(3, $emailadd);
    $stmt->bindValue(4, $mobile);
    $stmt->execute();
}
catch(Exception $e) {
    die(var_dump($e));
}
echo "<h3>Your're registered!</h3>";
}
	    
         
}


    function retrieve()
    {
        try {
        $conn = new PDO ("sqlsrv:server = tcp:djnrgfij6m.database.windows.net,1433; Database = Grocery-Express", "gexpress", "Groceryexpress2014"); 
        echo"retrieve".'<br/>';
        $sql_select = "SELECT * FROM registeredusers";
        $stmt = $conn->query($sql_select);
        $registered = $stmt->fetchAll(); 
        if(count($registered) > 0) {
        echo "<h2>People who are registered:</h2>";
        echo "<table>";
        echo "<tr><th>Username</th>";
        echo "<th>Password</th>";
        echo "<th>Email</th></tr>";
        echo "<th>Mobile No</th></tr>";
        foreach($registered as $user) {
            echo "<tr><td>".$user['username']."</td>";
            echo "<td>".$user['password']."</td>";
            echo "<td>".$user['emailadd']."</td></tr>";
            echo "<td>".$user['mobileno']."</td></tr>";
        }
        echo "</table>";
         }
        }
        catch(PDOException $e)
        {
            echo $sql . "<br>" . $e->getMessage();
        }
    }
?>

